using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class AdminDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procAdmin_add]";
        private string _strSPUpdateName = "dbo.[procAdmin_update]";
        private string _strSPDeleteName = "dbo.[procAdmin_delete]";
        private string _strSPGetName = "dbo.[procAdmin_get]";
        private string _strSPGetAllName = "dbo.[procAdmin_getall]";
        private string _strSPGetPages = "dbo.[procAdmin_getpaged]";
        private string _strSPIsExist = "dbo.[procAdmin_isexist]";
        private string _strTableName = "[Admin]";
        private string _strSPGetTransferOutName = "dbo.[procAdmin_gettransferout]";
        #endregion Local Variable

        #region Method
        public AdminInfo Get(
        Int32 ID,
        ref string sErr)
        {
            AdminInfo objEntr = new AdminInfo();
            connect();
            InitSPCommand(_strSPGetName);
            AddParameter(AdminInfo.Field.ID.ToString(), ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP(command);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();

            if (list.Rows.Count > 0)
                objEntr = (AdminInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            AdminInfo result = new AdminInfo();
            result.ID = (dt.Rows[i][AdminInfo.Field.ID.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][AdminInfo.Field.ID.ToString()]));
            result.Username = (dt.Rows[i][AdminInfo.Field.Username.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][AdminInfo.Field.Username.ToString()]));
            result.Password = (dt.Rows[i][AdminInfo.Field.Password.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][AdminInfo.Field.Password.ToString()]));
            result.FullName = (dt.Rows[i][AdminInfo.Field.FullName.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][AdminInfo.Field.FullName.ToString()]));
            result.Email = (dt.Rows[i][AdminInfo.Field.Email.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][AdminInfo.Field.Email.ToString()]));

            return result;
        }

        public DataTable GetAll(
        ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetAllName);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(AdminInfo objEntr, ref string sErr)
        {
            int ret = -1;
            connect();
            InitSPCommand(_strSPInsertName);
            AddParameter(AdminInfo.Field.ID.ToString(), objEntr.ID);
            AddParameter(AdminInfo.Field.Username.ToString(), objEntr.Username);
            AddParameter(AdminInfo.Field.Password.ToString(), objEntr.Password);
            AddParameter(AdminInfo.Field.FullName.ToString(), objEntr.FullName);
            AddParameter(AdminInfo.Field.Email.ToString(), objEntr.Email);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeSPScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(AdminInfo objEntr)
        {
            connect();
            InitSPCommand(_strSPUpdateName);
            AddParameter(AdminInfo.Field.ID.ToString(), objEntr.ID);
            AddParameter(AdminInfo.Field.Username.ToString(), objEntr.Username);
            AddParameter(AdminInfo.Field.Password.ToString(), objEntr.Password);
            AddParameter(AdminInfo.Field.FullName.ToString(), objEntr.FullName);
            AddParameter(AdminInfo.Field.Email.ToString(), objEntr.Email);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        Int32 ID
        )
        {
            connect();
            InitSPCommand(_strSPDeleteName);
            AddParameter(AdminInfo.Field.ID.ToString(), ID);

            string sErr = "";
            try
            {
                excuteSPNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public DataTableCollection Get_Page(AdminInfo obj, string orderBy, int pageIndex, int pageSize, ref String sErr)
        {
            string whereClause = CreateWhereClause(obj);
            DataTableCollection dtList = null;
            connect();
            InitSPCommand(_strSPGetPages);

            AddParameter("WhereClause", whereClause);
            AddParameter("OrderBy", orderBy);
            AddParameter("PageIndex", pageIndex);
            AddParameter("PageSize", pageSize);

            try
            {
                dtList = executeCollectSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            return dtList;
        }

        public Boolean IsExist(
        Int32 ID
        )
        {
            connect();
            InitSPCommand(_strSPIsExist);
            AddParameter(AdminInfo.Field.ID.ToString(), ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(AdminInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            connect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            connect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelectSP();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            disconnect();


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public int IsExist(String Username)
        {
            string sErr = "";
            string query = "Select MAX([ID]) From " + _strTableName + " Where Username='" + Username + "'";
            connect();
            int id = -1;
            try
            {
                id = Convert.ToInt32(executeScalar(query));
            }
            catch (Exception ex)
            {
                id = -1;
                sErr = ex.Message;
            }
            if (id == 0)
                id = -1;
            if (sErr != "") ErrorLog.SetLog(sErr);
            return id;
        }
        #endregion Method

    }
}
